import os

from flask import Flask
from flask_caching import Cache

cache = Cache()
# 应用工厂函数
def create_app(test_config=None):
    # create and configure the app
    # __name__ 参数可以将当前模组信息传递给 flask 对象，比如路径信息等
    #
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )
    app.debug = 1;
    if test_config is None:
        # load the instance config, if ite exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    cache.init_app(app, config={'CACHE_TYPE': 'redis',          # Use Redis
                                'CACHE_REDIS_HOST': 'loclhost',  # Host, default 'localhost'
                                'CACHE_REDIS_PORT': 6379,       # Port, default 6379
                                'CACHE_REDIS_URL':'redis://localhost:6379'} )

    # a simple page the says hello
    @app.route('/hello')
    def hello():
        return 'Hello , world!'
    """
    # 将数据库操作注册到应用中
    from . import db
    db.init_app(app)

    # 注册 auth 蓝图
    from . import auth
    app.register_blueprint(auth.bp)
    """
    from flaskr.api import bp as api_bp
    app.register_blueprint(api_bp)



    return app

from . import cn_database
